package com.bayleaf.web.encoding;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;


import com.bayleaf.common.encoding.EncodeMethod;
import com.bayleaf.common.encoding.JHConverterImpl;
import com.bayleaf.common.encoding.CharsetType;
import com.bayleaf.core.BayleafServlet;
import com.bayleaf.core.Model;

@WebServlet(name="encoding",
            urlPatterns={"/encoding"},
            initParams={ @WebInitParam(name="title", value="EncodingConverter"), @WebInitParam(name="jsp", value="/encoding/EncodingConverter") } )
public class EncodingServlet extends BayleafServlet {

	public String process(Model model) throws Exception {
		JHConverterImpl converter = new JHConverterImpl();

		System.out.println("input:"+model.getString("input"));
//		System.out.println(model.getString("option"));
//		System.out.println(model.getString("u2e"));
//		System.out.println(model.getString("e2u"));
		
		String inputString = model.getString("input");
		String ouputString = "";
	
		 // euc-kr 류 => utf-8로 변환
		 if ("on".equals(model.getString("e2u"))){
			System.out.println(converter.convertEuckrToUTF8(inputString.getBytes(), CharsetType.EUC_KR, true));
        	model.setAttribute("euckr2utf8_f",converter.convertEuckrToUTF8(inputString.getBytes(), CharsetType.EUC_KR, false));
        	model.setAttribute("euckr2utf8_t",converter.convertEuckrToUTF8(inputString.getBytes(), CharsetType.EUC_KR, true));
        	model.setAttribute("ms2utf8_f",converter.convertEuckrToUTF8(inputString.getBytes(), CharsetType.MS949, false));
        	model.setAttribute("ms2utf8_t",converter.convertEuckrToUTF8(inputString.getBytes(), CharsetType.MS949, true));
  		
        	// utf-8 => euc-kr로 변환
		 }else if("on".equals(model.getString("u2e"))){
			//byte[] bResult = converter.convertUTF8ToEuckr(inputString, CharsetType.MS949, EncodeMethod.NCR_EUCKR);
			model.setAttribute("utf82euckr_a",new String(converter.convertUTF8ToEuckr(inputString, CharsetType.EUC_KR, EncodeMethod.ALL_NCR),"EUC-KR"));
			model.setAttribute("utf82euckr_n",new String(converter.convertUTF8ToEuckr(inputString, CharsetType.EUC_KR, EncodeMethod.NCR_EUCKR),"EUC-KR"));
			model.setAttribute("utf82euckr_e",new String(converter.convertUTF8ToEuckr(inputString, CharsetType.EUC_KR, EncodeMethod.EUCKR),"EUC-KR"));
			model.setAttribute("utf82euckr_i",new String(converter.convertUTF8ToEuckr(inputString, CharsetType.EUC_KR, EncodeMethod.IGNORE),"EUC-KR"));

  
			model.setAttribute("utf82ms_a",new String(converter.convertUTF8ToEuckr(inputString, CharsetType.MS949, EncodeMethod.ALL_NCR),"MS949"));
			model.setAttribute("utf82ms_n",new String(converter.convertUTF8ToEuckr(inputString, CharsetType.MS949, EncodeMethod.NCR_EUCKR),"MS949"));
			model.setAttribute("utf82ms_e",new String(converter.convertUTF8ToEuckr(inputString, CharsetType.MS949, EncodeMethod.EUCKR),"MS949"));
			model.setAttribute("utf82ms_i",new String(converter.convertUTF8ToEuckr(inputString, CharsetType.MS949, EncodeMethod.IGNORE),"MS949"));

		 }
    	return null;
	}
	

}
